package org.boyan.modules.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

import org.apache.shiro.SecurityUtils;
import org.boyan.common.system.api.ISysBaseAPI;
import org.boyan.common.system.util.JeecgDataAutorUtils;
import org.boyan.common.system.vo.LoginUser;
import org.boyan.common.util.oConvertUtils;
import org.boyan.modules.auth.service.IOnlAuthDataService;
import org.boyan.modules.auth.service.IOnlAuthPageService;
import org.boyan.modules.cgform.a.a;
import org.boyan.modules.cgform.entity.OnlCgformField;
import org.boyan.modules.cgform.entity.OnlCgformHead;
import org.boyan.modules.cgform.mapper.OnlCgformFieldMapper;
import org.boyan.modules.cgform.mapper.OnlCgformHeadMapper;
import org.boyan.modules.cgform.model.TreeModel;
import org.boyan.modules.cgform.model.e;
import org.boyan.modules.cgform.service.IOnlCgformFieldService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("onlCgformFieldServiceImpl")
public class d extends ServiceImpl<OnlCgformFieldMapper, OnlCgformField> implements IOnlCgformFieldService {
    private static final Logger a = LoggerFactory.getLogger(d.class);

    @Autowired
    private OnlCgformFieldMapper onlCgformFieldMapper;

    @Autowired
    private OnlCgformHeadMapper cgformHeadMapper;

    @Autowired
    private IOnlAuthDataService onlAuthDataService;

    @Autowired
    private IOnlAuthPageService onlAuthPageService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static final String b = "0";

    @Override
    public Map<String, Object> queryAutolistPage(String tbname, String headId, Map<String, Object> params, List<String> needList) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, headId);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> list1 = list(lambdaQueryWrapper);
        List<OnlCgformField> list2 = queryAvailableFields(headId, tbname, true, list1, needList);
        StringBuffer stringBuffer = new StringBuffer();
        org.boyan.modules.cgform.util.b.a(tbname, list2, stringBuffer);
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str1 = loginUser.getId();
        List list = this.onlAuthDataService.queryUserOnlineAuthData(str1, headId);
        if (list != null && list.size() > 0) {
            JeecgDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(loginUser.getUsername()));
        }
        String str2 = org.boyan.modules.cgform.util.b.a(list1, params, needList, list);
        String str3 = org.boyan.modules.cgform.util.b.a(params);
        stringBuffer.append(" where 1=1  " + str2 + str3);
        String str4 = a(list1, params);
        stringBuffer.append(str4);
        Integer integer = (params.get("pageSize") == null) ? 10 : Integer.parseInt(params.get("pageSize").toString());
        if (integer == -521) {
            List list3 = this.onlCgformFieldMapper.queryListBySql(stringBuffer.toString());
            a.debug("---Online查询sql 不分页 :>>" + stringBuffer.toString());
            if (list3 == null || list3.size() == 0) {
                hashMap.put("total", Integer.valueOf(0));
                hashMap.put("fieldList", list2);
            } else {
                hashMap.put("total", Integer.valueOf(list3.size()));
                hashMap.put("fieldList", list2);
                hashMap.put("records", org.boyan.modules.cgform.util.b.d(list3));
            }
        } else {
            Integer integer1 = (params.get("pageNo") == null) ? 1 : Integer.parseInt(params.get("pageNo").toString());
            Page page = new Page(integer1.intValue(), integer.intValue());
            a.debug("---Online查询sql:>>" + stringBuffer.toString());
            IPage iPage = this.onlCgformFieldMapper.selectPageBySql(page, stringBuffer.toString());
            hashMap.put("total", Long.valueOf(iPage.getTotal()));
            hashMap.put("records", org.boyan.modules.cgform.util.b.d(iPage.getRecords()));
        }
        return (Map)hashMap;
    }

    @Override
    public Map<String, Object> queryAutoTreeNoPage(String tbname, String headId, Map<String, Object> params, List<String> needList, String pidField) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, headId);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> list1 = list(lambdaQueryWrapper);
        List<OnlCgformField> list2 = queryAvailableFields(headId, tbname, true, list1, needList);
        StringBuffer stringBuffer = new StringBuffer();
        org.boyan.modules.cgform.util.b.a(tbname, list2, stringBuffer);
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str1 = loginUser.getId();
        List list = this.onlAuthDataService.queryUserOnlineAuthData(str1, headId);
        if (list != null && list.size() > 0) {
            JeecgDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(loginUser.getUsername()));
        }
        String str2 = org.boyan.modules.cgform.util.b.a(list1, params, needList, list);
        String str3 = org.boyan.modules.cgform.util.b.a(params);
        stringBuffer.append(" where 1=1  ").append(str2).append(str3);
        String str4 = a(list1, params);
        stringBuffer.append(str4);
        int integer = (params.get("pageSize") == null) ? 10 : Integer.parseInt(params.get("pageSize").toString());
        if (integer == -521) {
            List<Map<String, Object>> list3 = this.onlCgformFieldMapper.queryListBySql(stringBuffer.toString());
            if ("true".equals(params.get("hasQuery"))) {
                ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
                for (Map<String, Object> map : list3) {
                    String str = map.get(pidField).toString();
                    if (str != null && !"0".equals(str)) {
                        Map<String, Object> map1 = a(str, tbname, headId, needList, pidField);
                        if (map1 != null && map1.size() > 0 && !arrayList.contains(map1))
                            arrayList.add(map1);
                        continue;
                    }
                    if (!arrayList.contains(map))
                        arrayList.add(map);
                }
                list3 = arrayList;
            }
            a.debug("---Online查询sql 不分页 :>>" + stringBuffer.toString());
            if (list3 == null || list3.size() == 0) {
                hashMap.put("total", 0);
                hashMap.put("fieldList", list2);
            } else {
                hashMap.put("total", list3.size());
                hashMap.put("fieldList", list2);
                hashMap.put("records", org.boyan.modules.cgform.util.b.d(list3));
            }
        } else {
            Integer integer1 = (params.get("pageNo") == null) ? 1 : Integer.parseInt(params.get("pageNo").toString());
            Page page = new Page(integer1, (int) integer);
            a.debug("---Online查询sql:>>" + stringBuffer.toString());
            IPage iPage = this.onlCgformFieldMapper.selectPageBySql(page, stringBuffer.toString());
            hashMap.put("total", iPage.getTotal());
            hashMap.put("records", org.boyan.modules.cgform.util.b.d(iPage.getRecords()));
        }
        return (Map)hashMap;
    }

    private Map<String, Object> a(String paramString1, String paramString2, String paramString3, List<String> paramList, String paramString4) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", paramString1);
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, paramString3);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> list1 = list(lambdaQueryWrapper);
        List<OnlCgformField> list2 = queryAvailableFields(paramString3, paramString2, true, list1, paramList);
        StringBuffer stringBuffer = new StringBuffer();
        org.boyan.modules.cgform.util.b.a(paramString2, list2, stringBuffer);
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str1 = loginUser.getId();
        List list = this.onlAuthDataService.queryUserOnlineAuthData(str1, paramString3);
        if (list != null && list.size() > 0) {
            JeecgDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(loginUser.getUsername()));
        }
        String str2 = org.boyan.modules.cgform.util.b.a(list1, hashMap, paramList, list);
        stringBuffer.append(" where 1=1  ").append(str2).append("and id='").append(paramString1).append("'");
        List<Map<String, Object>> list3 = this.onlCgformFieldMapper.queryListBySql(stringBuffer.toString());
        if (list3 != null && list3.size() > 0) {
            Map<String, Object> map = list3.get(0);
            if (map != null && map.get(paramString4) != null && !"0".equals(map.get(paramString4))) {
                return a(map.get(paramString4).toString(), paramString2, paramString3, paramList, paramString4);
            }
            return map;
        }
        return null;
    }

    @Override
    public void saveFormData(String code, String tbname, JSONObject json, boolean isCrazy) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        if (isCrazy) {
            this.baseMapper.executeInsertSQL(org.boyan.modules.cgform.util.b.c(tbname, list, json));
        } else {
            this.baseMapper.executeInsertSQL(org.boyan.modules.cgform.util.b.a(tbname, list, json));
        }
    }

    @Override
    public void saveTreeFormData(String code, String tbname, JSONObject json, String hasChildField, String pidField) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        for (OnlCgformField onlCgformField : list) {
            if (hasChildField.equals(onlCgformField.getDbFieldName()) && onlCgformField.getIsShowForm() != 1) {
                onlCgformField.setIsShowForm(1);
                json.put(hasChildField, "0");
                continue;
            }
            if (pidField.equals(onlCgformField.getDbFieldName()) && oConvertUtils.isEmpty(json.get(pidField))) {
                onlCgformField.setIsShowForm(1);
                json.put(pidField, "0");
            }
        }
        Map map = org.boyan.modules.cgform.util.b.a(tbname, list, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeInsertSQL(map);
        if (!"0".equals(json.getString(pidField))) {
            ((OnlCgformFieldMapper)this.baseMapper).editFormData("update " + tbname + " set " + hasChildField + " = '1' where id = '" + json.getString(pidField) + "'");
        }
    }

    @Override
    public void saveFormData(List<OnlCgformField> fieldList, String tbname, JSONObject json) {
        Map map = org.boyan.modules.cgform.util.b.a(tbname, fieldList, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeInsertSQL(map);
    }

    @Override
    public void editFormData(String code, String tbname, JSONObject json, boolean isCrazy) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        List list = list(lambdaQueryWrapper);
        if (isCrazy) {
            this.baseMapper.executeUpdatetSQL(org.boyan.modules.cgform.util.b.d(tbname, list, json));
        } else {
            this.baseMapper.executeUpdatetSQL(org.boyan.modules.cgform.util.b.b(tbname, list, json));
        }
    }

    @Override
    public void editTreeFormData(String code, String tbname, JSONObject json, String hasChildField, String pidField) {
        String str1 = org.boyan.modules.cgform.util.b.f(tbname);
        String str2 = "select * from " + str1 + " where id = '" + json.getString("id") + "'";
        Map<String, Object> map1 = this.baseMapper.queryFormData(str2);
        Map<String, Object> map2 = org.boyan.modules.cgform.util.b.b(map1);
        String str3 = map2.get(pidField).toString();
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        for (OnlCgformField onlCgformField : list) {
            if (pidField.equals(onlCgformField.getDbFieldName()) && oConvertUtils.isEmpty(json.get(pidField))) {
                onlCgformField.setIsShowForm(1);
                json.put(pidField, "0");
            }
        }
        Map<String, Object> map3 = org.boyan.modules.cgform.util.b.b(tbname, list, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeUpdatetSQL(map3);
        if (!str3.equals(json.getString(pidField))) {
            if (!"0".equals(str3)) {
                String str = "select count(*) from " + str1 + " where " + pidField + " = '" + str3 + "'";
                Integer integer = ((OnlCgformFieldMapper)this.baseMapper).queryCountBySql(str);
                if (integer == null || integer == 0) {
                    ((OnlCgformFieldMapper)this.baseMapper).editFormData("update " + str1 + " set " + hasChildField + " = '0' where id = '" + str3 + "'");
                }
            }
            if (!"0".equals(json.getString(pidField))) {
                ((OnlCgformFieldMapper)this.baseMapper).editFormData("update " + str1 + " set " + hasChildField + " = '1' where id = '" + json.getString(pidField) + "'");
            }
        }
    }

    @Override
    public Map<String, Object> queryFormData(String code, String tbname, String id) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        lambdaQueryWrapper.eq(OnlCgformField::getIsShowForm, 1);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        String str = org.boyan.modules.cgform.util.b.a(tbname, list, id);
        return this.onlCgformFieldMapper.queryFormData(str);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteAutoListMainAndSub(OnlCgformHead head, String ids) {
        if (head.getTableType().intValue() == 2) {
            String str1 = head.getId();
            String str2 = head.getTableName();
            String str3 = "tableName";
            String str4 = "linkField";
            String str5 = "linkValueStr";
            String str6 = "mainField";
            ArrayList<HashMap<Object, Object>> arrayList = new ArrayList();
            if (oConvertUtils.isNotEmpty(head.getSubTableStr())) {
                for (String str : head.getSubTableStr().split(",")) {
                    OnlCgformHead onlCgformHead = this.cgformHeadMapper.selectOne(
                            new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, str));
                    if (onlCgformHead != null) {
                        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper1 = new LambdaQueryWrapper<OnlCgformField>()
                                .eq(OnlCgformField::getCgformHeadId, onlCgformHead.getId())
                                .eq(OnlCgformField::getMainTable, head.getTableName());
                        List<OnlCgformField> list1 = list(lambdaQueryWrapper1);
                        if (list1 != null && list1.size() != 0) {
                            OnlCgformField onlCgformField = list1.get(0);
                            HashMap<Object, Object> hashMap = new HashMap<>();
                            hashMap.put(str4, onlCgformField.getDbFieldName());
                            hashMap.put(str6, onlCgformField.getMainField());
                            hashMap.put(str3, str);
                            hashMap.put(str5, "");
                            arrayList.add(hashMap);
                        }
                    }
                }
                LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, str1);
                List<OnlCgformField> list = list(lambdaQueryWrapper);
                String[] arrayOfString = ids.split(",");
                for (String str7 : arrayOfString) {
                    String str8 = org.boyan.modules.cgform.util.b.a(str2, list, str7);
                    Map<String, Object> map = this.onlCgformFieldMapper.queryFormData(str8);
                    ArrayList arrayList1 = new ArrayList();
                    for (HashMap<Object, Object> map1 : arrayList) {
                        Object object = map.get(((String)map1.get(str6)).toLowerCase());
                        if (object == null) {
                            object = map.get(((String)map1.get(str6)).toUpperCase());
                        }
                        if (object == null) {
                            continue;
                        }
                        String str = (String)map1.get(str5) + String.valueOf(object) + ",";
                        map1.put(str5, str);
                    }
                }
                for (Map<Object, Object> map : arrayList) {
                    deleteAutoList((String)map.get(str3), (String)map.get(str4), (String)map.get(str5));
                }
            }
            deleteAutoListById(head.getTableName(), ids);
        }
    }

    @Override
    public void deleteAutoListById(String tbname, String ids) {
        deleteAutoList(tbname, "id", ids);
    }

    @Override
    public void deleteAutoList(String tbname, String linkField, String linkValue) {
        if (linkValue != null && !"".equals(linkValue)) {
            String[] arrayOfString = linkValue.split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : arrayOfString) {
                if (str != null && !"".equals(str)) {
                    stringBuffer.append("'" + str + "',");
                }
            }
            String str1 = stringBuffer.toString();
            String str2 = "DELETE FROM " + org.boyan.modules.cgform.util.b.f(tbname) + " where " + linkField + " in(" + str1.substring(0, str1.length() - 1) + ")";
            a.debug("--删除sql-->" + str2);
            this.onlCgformFieldMapper.deleteAutoList(str2);
        }
    }

    @Override
    public List<Map<String, String>> getAutoListQueryInfo(String code) {
        int i = 0;
        OnlCgformHead onlCgformHead = (OnlCgformHead)this.cgformHeadMapper.selectOne(new LambdaQueryWrapper<OnlCgformHead>()
                .eq(OnlCgformHead::getId, code));
        ArrayList<Map<String, String>> arrayList = new ArrayList();
        boolean bool = org.boyan.modules.cgform.util.b.a(onlCgformHead);
        i = a(onlCgformHead, arrayList, i, bool);
        Integer integer = onlCgformHead.getTableType();
        if (bool && integer != null && 2 == integer.intValue()) {
            String str = onlCgformHead.getSubTableStr();
            if (str != null && !"".equals(str)) {
                String[] arrayOfString = str.split(",");
                for (String str1 : arrayOfString) {
                    OnlCgformHead onlCgformHead1 = this.cgformHeadMapper.selectOne(
                            new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, str1));
                    if (onlCgformHead1 != null)
                        i = a(onlCgformHead1, arrayList, i, true);
                }
            }
        }
        return arrayList;
    }

    @Override
    public List<OnlCgformField> queryFormFields(String code, boolean isform) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, code);
        if (isform)
            lambdaQueryWrapper.eq(OnlCgformField::getIsShowForm, 1);
        return list(lambdaQueryWrapper);
    }

    @Override
    public List<OnlCgformField> queryFormFieldsByTableName(String tableName) {
        OnlCgformHead onlCgformHead = this.cgformHeadMapper.selectOne(new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, tableName));
        if (onlCgformHead != null) {
            LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, onlCgformHead.getId());
            return list(lambdaQueryWrapper);
        }
        return null;
    }

    @Override
    public OnlCgformField queryFormFieldByTableNameAndField(String tableName, String fieldName) {
        OnlCgformHead onlCgformHead = (OnlCgformHead)this.cgformHeadMapper.selectOne(new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, tableName));
        if (onlCgformHead != null) {
            LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, onlCgformHead.getId());
            lambdaQueryWrapper.eq(OnlCgformField::getDbFieldName, fieldName);
            if (list(lambdaQueryWrapper) != null && list(lambdaQueryWrapper).size() > 0)
                return list(lambdaQueryWrapper).get(0);
        }
        return null;
    }

    @Override
    public Map<String, Object> queryFormData(List<OnlCgformField> fieldList, String tbname, String id) {
        String str = org.boyan.modules.cgform.util.b.a(tbname, fieldList, id);
        return this.onlCgformFieldMapper.queryFormData(str);
    }

    @Override
    public List<Map<String, Object>> querySubFormData(List<OnlCgformField> fieldList, String tbname, String linkField, String value) {
        String str = org.boyan.modules.cgform.util.b.a(tbname, fieldList, linkField, value);
        return this.onlCgformFieldMapper.queryListData(str);
    }

    @Override
    public IPage<Map<String, Object>> selectPageBySql(Page<Map<String, Object>> page, String sql) {
        return this.baseMapper.selectPageBySql(page, sql);
    }

    @Override
    public List<String> selectOnlineHideColumns(String tbname) {
        String str1 = "online:" + tbname + ":%";
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str2 = loginUser.getId();
        List<String> list = ((OnlCgformFieldMapper)this.baseMapper).selectOnlineHideColumns(str2, str1);
        return a(list);
    }

    @Override
    public List<OnlCgformField> queryAvailableFields(String cgFormId, String tbname, String taskId, boolean isList) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, cgFormId);
        if (isList) {
            lambdaQueryWrapper.eq(OnlCgformField::getIsShowList, 1);
        } else {
            lambdaQueryWrapper.eq(OnlCgformField::getIsShowForm, 1);
        }
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        String str1 = "online:" + tbname + "%";
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str2 = loginUser.getId();
        ArrayList<String> arrayList = new ArrayList<>();
        if (oConvertUtils.isEmpty(taskId)) {
            List<String> list1 = this.onlAuthPageService.queryHideCode(str2, cgFormId, isList);
            if (list1 != null && list1.size() != 0 && list1.get(0) != null)
                arrayList.addAll(list1);
        } else if (oConvertUtils.isNotEmpty(taskId)) {
            List<String> list1 = this.baseMapper.selectFlowAuthColumns(tbname, taskId, "1");
            if (list1 != null && list1.size() > 0 && list1.get(0) != null)
                arrayList.addAll(list1);
        }
        if (arrayList.size() == 0)
            return list;
        ArrayList<OnlCgformField> arrayList1 = new ArrayList<>();
        for (OnlCgformField onlCgformField : list) {
            if (b(onlCgformField.getDbFieldName(), arrayList))
                arrayList1.add(onlCgformField);
        }
        return arrayList1;
    }

    @Override
    public List<String> queryDisabledFields(String tbname) {
        String str1 = "online:" + tbname + "%";
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str2 = loginUser.getId();
        List<String> list = this.baseMapper.selectOnlineDisabledColumns(str2, str1);
        return a(list);
    }

    @Override
    public List<String> queryDisabledFields(String tbname, String taskId) {
        if (oConvertUtils.isEmpty(taskId)) {
            return null;
        }
        List<String> list = this.baseMapper.selectFlowAuthColumns(tbname, taskId, "2");
        return a(list);
    }

    private List<String> a(List<String> paramList) {
        ArrayList<String> arrayList = new ArrayList<>();
        if (paramList == null || paramList.size() == 0 || paramList.get(0) == null) {
            return arrayList;
        }
        for (String str1 : paramList) {
            if (oConvertUtils.isEmpty(str1)) {
                continue;
            }
            String str2 = str1.substring(str1.lastIndexOf(":") + 1);
            if (oConvertUtils.isEmpty(str2)) {
                continue;
            }
            arrayList.add(str2);
        }
        return arrayList;
    }

    @Override
    public List<OnlCgformField> queryAvailableFields(String tbname, boolean isList, List<OnlCgformField> list, List<String> needList) {
        String str1 = "online:" + tbname + "%";
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str2 = loginUser.getId();
        List<String> list1 = this.baseMapper.selectOnlineHideColumns(str2, str1);
        return a(list1, isList, list, needList);
    }

    @Override
    public List<OnlCgformField> queryAvailableFields(String cgformId, String tbname, boolean isList, List<OnlCgformField> list, List<String> needList) {
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String str = loginUser.getId();
        List<String> list1 = this.onlAuthPageService.queryListHideColumn(str, cgformId);
        return a(list1, isList, list, needList);
    }

    private List<OnlCgformField> a(List<String> paramList1, boolean paramBoolean, List<OnlCgformField> paramList, List<String> paramList2) {
        ArrayList<OnlCgformField> arrayList = new ArrayList<>();
        boolean bool = paramList1 != null && paramList1.size() != 0 && paramList1.get(0) != null;
        for (OnlCgformField onlCgformField : paramList) {
            String str = onlCgformField.getDbFieldName();
            if (paramList2 != null && paramList2.contains(str)) {
                onlCgformField.setIsQuery(1);
                arrayList.add(onlCgformField);
                continue;
            }
            if (paramBoolean) {
                if (onlCgformField.getIsShowList() != 1) {
                    if (oConvertUtils.isNotEmpty(onlCgformField.getMainTable()) && oConvertUtils.isNotEmpty(onlCgformField.getMainField())) {
                        arrayList.add(onlCgformField);
                    }
                    continue;
                }
            } else if (onlCgformField.getIsShowForm() != 1) {
                continue;
            }
            if (bool) {
                if (b(str, paramList1)) {
                    arrayList.add(onlCgformField);
                }
                continue;
            }
            arrayList.add(onlCgformField);
        }
        return arrayList;
    }

    private boolean b(String paramString, List<String> paramList) {
        boolean bool = true;
        for (String str : paramList) {
            if (!oConvertUtils.isEmpty(str)) {
                String str1 = str.substring(str.lastIndexOf(":") + 1);
                if (!oConvertUtils.isEmpty(str1)) {
                    if (str1.equals(paramString)) {
                        bool = false;
                    }
                }
            }
        }
        return bool;
    }

    public boolean a(String paramString, List<OnlCgformField> paramList) {
        boolean bool = false;
        for (OnlCgformField onlCgformField : paramList) {
            if (oConvertUtils.camelToUnderline(paramString).equals(onlCgformField.getDbFieldName())) {
                bool = true;
                break;
            }
        }
        return bool;
    }

    @Override
    public void executeInsertSQL(Map<String, Object> params) {
        ((OnlCgformFieldMapper)this.baseMapper).executeInsertSQL(params);
    }

    @Override
    public void executeUpdatetSQL(Map<String, Object> params) {
        ((OnlCgformFieldMapper)this.baseMapper).executeUpdatetSQL(params);
    }

    @Override
    public List<TreeModel> queryDataListByLinkDown(a linkDown) {
        return ((OnlCgformFieldMapper)this.baseMapper).queryDataListByLinkDown(linkDown);
    }

    @Override
    public void updateTreeNodeNoChild(String tableName, String filed, String id) {
        Map<String, Object> map = org.boyan.modules.cgform.util.b.a(tableName, filed, id);
        this.baseMapper.executeUpdatetSQL(map);
    }

    @Override
    public String queryTreeChildIds(OnlCgformHead head, String ids) {
        String str1 = head.getTreeParentIdField();
        String str2 = head.getTableName();
        String[] arrayOfString = ids.split(",");
        StringBuffer stringBuffer = new StringBuffer();
        for (String str : arrayOfString) {
            if (str != null &&
                    !stringBuffer.toString().contains(str)) {
                if (stringBuffer.toString().length() > 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append(str);
                a(str, str1, str2, stringBuffer);
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public String queryTreePids(OnlCgformHead head, String ids) {
        String str1 = head.getTreeParentIdField();
        String str2 = head.getTableName();
        StringBuilder stringBuffer = new StringBuilder();
        String[] arrayOfString = ids.split(",");
        for (String str : arrayOfString) {
            if (str != null) {
                String str3 = org.boyan.modules.cgform.util.b.f(str2);
                String str4 = "select * from " + str3 + " where id = '" + str + "'";
                Map<String, Object> map1 = this.baseMapper.queryFormData(str4);
                Map<String, Object> map2 = org.boyan.modules.cgform.util.b.b(map1);
                String str5 = map2.get(str1).toString();
                String str6 = "select * from " + org.boyan.modules.cgform.util.b.f(str2) + " where " + str1 + "= '" + str5 + "' and id not in(" + ids + ")";
                List<Map<String, Object>> list = this.onlCgformFieldMapper.queryListBySql(str6);
                if ((list == null || list.size() == 0) && !Arrays.<String>asList(arrayOfString).contains(str5) &&
                        !stringBuffer.toString().contains(str5)) {
                    stringBuffer.append(str5).append(",");
                }
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public String queryForeignKey(String cgFormId, String mainTable) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, cgFormId);
        lambdaQueryWrapper.eq(OnlCgformField::getMainTable, mainTable);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        if (list != null && list.size() > 0) {
            return ((OnlCgformField)list.get(0)).getMainField();
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> queryListBySql(String sql) {
        return this.onlCgformFieldMapper.queryListBySql(sql);
    }

    @Override
    public IPage<Map<String, Object>> selectPageBySql(String sql, int pageNo, int pageSize) {
        Page<Map<String, Object>> page = new Page<>(pageNo, pageSize);
        return this.onlCgformFieldMapper.selectPageBySql(page, sql);
    }

    private StringBuffer a(String paramString1, String paramString2, String paramString3, StringBuffer paramStringBuffer) {
        String str = "select * from " + org.boyan.modules.cgform.util.b.f(paramString3) + " where " + paramString2 + "= '" + paramString1 + "'";
        List<Map<String,Object>> list = this.onlCgformFieldMapper.queryListBySql(str);
        if (list != null && list.size() > 0) {
            for (Map<String,Object> map1 : list) {
                Map<String, Object> map2 = org.boyan.modules.cgform.util.b.b(map1);
                if (!paramStringBuffer.toString().contains(map2.get("id").toString())) {
                    paramStringBuffer.append(",").append(map2.get("id"));
                }
                a(map2.get("id").toString(), paramString2, paramString3, paramStringBuffer);
            }
        }
        return paramStringBuffer;
    }

    private String a(List<OnlCgformField> paramList, Map<String, Object> paramMap) {
        Object object = paramMap.get("column");
        ArrayList<e> arrayList = new ArrayList<>();
        if (object != null && !"id".equals(object.toString())) {
            String str1 = object.toString();
            Object object1 = paramMap.get("order");
            String str2 = "desc";
            if (object1 != null) {
                str2 = object1.toString();
            }
            e e = new e(str1, str2);
            arrayList.add(e);
        } else {
            for (OnlCgformField onlCgformField : paramList) {
                if ("1".equals(onlCgformField.getSortFlag())) {
                    String str = onlCgformField.getFieldExtendJson();
                    e e = new e(onlCgformField.getDbFieldName());
                    if (str != null && !"".equals(str)) {
                        JSONObject jSONObject = JSON.parseObject(str);
                        String str1 = jSONObject.getString("orderRule");
                        if (str1 != null && !"".equals(str1)) {
                            e.setRule(str1);
                        }
                    }
                    arrayList.add(e);
                }
            }
            if (arrayList.size() == 0) {
                e e = org.boyan.modules.cgform.model.e.a();
                arrayList.add(e);
            }
        }
        ArrayList<String> arrayList1 = new ArrayList<>();
        for (e e : arrayList) {
            if (a(e.getColumn(), paramList)) {
                String str = e.getRealSql();
                arrayList1.add(str);
            }
        }
        return " ORDER BY " + String.join(",", arrayList1);
    }

    private int a(OnlCgformHead paramOnlCgformHead, List<Map<String, String>> paramList, int paramInt, boolean paramBoolean) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, paramOnlCgformHead.getId());
        lambdaQueryWrapper.eq(OnlCgformField::getIsQuery, 1);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> list = list(lambdaQueryWrapper);
        for (OnlCgformField onlCgformField : list) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("label", onlCgformField.getDbFieldTxt());
            if (paramBoolean) {
                hashMap.put("field", paramOnlCgformHead.getTableName() + "@" + onlCgformField.getDbFieldName());
            } else {
                hashMap.put("field", onlCgformField.getDbFieldName());
            }
            hashMap.put("dbField", onlCgformField.getDbFieldName());
            hashMap.put("mode", onlCgformField.getQueryMode());
            String str = onlCgformField.getQueryConfigFlag();
            if ("1".equals(str)) {
                String str1 = onlCgformField.getQueryShowType();
                hashMap.put("config", "1");
                hashMap.put("view", str1);
                hashMap.put("defValue", onlCgformField.getQueryDefVal());
                if ("cat_tree".equals(str1)) {
                    hashMap.put("pcode", onlCgformField.getQueryDictField());
                } else if ("sel_tree".equals(str1)) {
                    String[] arrayOfString = onlCgformField.getQueryDictText().split(",");
                    String str2 = onlCgformField.getQueryDictTable() + "," + arrayOfString[2] + "," + arrayOfString[0];
                    hashMap.put("dict", str2);
                    hashMap.put("pidField", arrayOfString[1]);
                    hashMap.put("hasChildField", arrayOfString[3]);
                    hashMap.put("pidValue", onlCgformField.getQueryDictField());
                } else {
                    hashMap.put("dictTable", onlCgformField.getQueryDictTable());
                    hashMap.put("dictCode", onlCgformField.getQueryDictField());
                    hashMap.put("dictText", onlCgformField.getQueryDictText());
                }
            } else {
                String str1 = onlCgformField.getFieldShowType();
                hashMap.put("view", str1);
                hashMap.put("mode", onlCgformField.getQueryMode());
                if ("cat_tree".equals(str1)) {
                    hashMap.put("pcode", onlCgformField.getDictField());
                } else if ("sel_tree".equals(str1)) {
                    String[] arrayOfString = onlCgformField.getDictText().split(",");
                    String str2 = onlCgformField.getDictTable() + "," + arrayOfString[2] + "," + arrayOfString[0];
                    hashMap.put("dict", str2);
                    hashMap.put("pidField", arrayOfString[1]);
                    hashMap.put("hasChildField", arrayOfString[3]);
                    hashMap.put("pidValue", onlCgformField.getDictField());
                } else {
                    hashMap.put("dictTable", onlCgformField.getDictTable());
                    hashMap.put("dictCode", onlCgformField.getDictField());
                    hashMap.put("dictText", onlCgformField.getDictText());
                }
            }
            if (++paramInt > 2) {
                hashMap.put("hidden", "1");
            }
            paramList.add(hashMap);
        }
        return paramInt;
    }
}